﻿using BCL.BaseLib;
using BCL.Tool.Extend;
using BCL.WPFComponent.Base;
using Devices;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Imaging;

using BCL.Tool;
using DevicesView.Logic.Test;
using DevicesView;
namespace DevicesView
{

    /// <summary>
    /// UCTestNew.xaml 的交互逻辑
    /// </summary>
    public partial class UCTestNew : BaseFunctionControl
    {


        private ResultLogic resultLogic;
        private List<BitmapImage> images;
        private List<UCTestResult> ucTestResults;
        public UCTestNew()
        {
            InitializeComponent();
        }
        public UCTestNew(FunctionEntity fe)
        {
            InitializeComponent();
            ucTestResults = new List<UCTestResult>();
        }

        public override void LanguageBinding()
        {
            throw new NotImplementedException();
        }
        public override void ReloadData(object data)
        {
            base.ReloadData(data);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            resultLogic = new DevicesView.Logic.Test.ResultLogic();
            images = new List<BitmapImage>
            {
                File.ReadAllBytes("image/icon_d4.png").ToBitmap(),
                File.ReadAllBytes("image/return.png").ToBitmap(),
                File.ReadAllBytes("image/restart.png").ToBitmap()
            };

            Devices.DevicesCollection.CommandCompleted += Item_CommandCompleted;
            Devices.DevicesCollection.DevicesCountChanged += DevicesCollection_DevicesCountChanged;

            DevicesCollection_DevicesCountChanged();


        }


        private void Item_CommandCompleted(Devices.Command sender, Devices.Result e)
        {
            if (e.ResultDatas == null)
                return;

        }
        /// <summary>
        /// 保存结果
        /// </summary>
        /// <param name="uc"></param>
        /// <param name="cmd"></param>
        private void Ucall_SaveCommandIng(UCTestResult uc, Command cmd)
        {

        }
        private void DevicesCollection_DevicesCountChanged()
        {
            this.MainTag.Items.Clear();
            List<Devices.IDevices> devs = Devices.DevicesCollection.Devices;

            this.Dispatcher.Invoke(new Action(() =>
            {
                TabItem pageall = new TabItem { Header = "全部".Language(), IsSelected = true, Style = (Style)this.FindResource("TabItemStyle") };
                UCTestResult ucall = new UCTestResult();
                ucall.SaveCommandIng += Ucall_SaveCommandIng;
                ucall.DeleteCommanded += Ucall_DeleteCommanded;
                pageall.Content = ucall;
                this.MainTag.Items.Add(pageall);
                ucTestResults.Add(ucall);
                devs?.ForEach(o =>
                {
                    TabItem page = new TabItem { Header = o.Info.DisplayName, Tag = o, Style = (Style)this.FindResource("TabItemStyle") };
                    UCTestResult ucdev = new UCTestResult(o);
                    ucdev.SaveCommandIng += Ucall_SaveCommandIng;
                    ucdev.DeleteCommanded += Ucall_DeleteCommanded;
                    page.Content = ucdev;
                    this.MainTag.Items.Add(page);
                    ucTestResults.Add(ucdev);
                });
            }));
        }

        private void Ucall_DeleteCommanded(UCTestResult arg1, Command cmd)
        {


        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           
        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }


        private bool SendCmd(TestInfo info)
        {
            Devices.IDevices device = null;
            if (info.CMD != null)
            {
                if (BCL.WPFComponent.UCMessageBox.Show("检查已完成，确定要重新发送嘛？".Language(), "提示".Language(), MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                    return false;

                foreach (TabItem item in MainTag.Items)
                    if ((IDevices)item.Tag == info.CMD.Devices || item.Tag == null)
                        (item.Content as UCTestResult).RemoveCmds(info.CMD);

                device = info.CMD.Devices;
                if (info.CMD.Devices != null)
                    info.CMD.Devices.RemoveCommand(info.CMD.Id);
                else
                {
                    Devices.Control.FormSelectDev form = new Devices.Control.FormSelectDev(true);
                    if (form.ShowDialog() != DialogResult.OK)
                        return false;
                    device = form.Device;
                }

            }
            else
            {
                Devices.Control.FormSelectDev form = new Devices.Control.FormSelectDev(true);
                if (form.ShowDialog() != DialogResult.OK)
                    return false;
                device = form.Device;
            }
            info.Devices = 0;

            Devices.Command cmd = new Devices.Command
            {
                Id = info.ReportId,
                Name = info.ReportName,
                PetId = info.PetId,
                PetName = info.PetName,
                Code = info.ReportId,
                Customer = info.CustomerName,
                Date = DateTime.Now,
                Doctor = info.PhysicianName,
                DoctorId = info.PhysicianId
            };
            TestPetInfo pet = logic.GetTestPet(info.ReportId);
            if (pet == null)
            {
                BCL.WPFComponent.UCMessageBox.Show("宠物不存在或者已被删除！");
                return false;
            }
            cmd.KindOf = Devices.KindOfType.犬;
            if (logic.StaticData.TryGetValue(pet.KindOF.ToString(), out string kindof))
            {
                switch (kindof)
                {
                    case "猫":
                        cmd.KindOf = Devices.KindOfType.猫;
                        break;
                    case "兔":
                        cmd.KindOf = Devices.KindOfType.兔;
                        break;
                    case "鼠":
                        cmd.KindOf = Devices.KindOfType.小鼠;
                        break;
                    case "龟":
                        cmd.KindOf = Devices.KindOfType.龟;
                        break;
                }
            }

            cmd.Gender = logic.StaticData[pet.Gender] == "母" ? Devices.GenderType.Female : Devices.GenderType.Male;
            cmd.Age = "1";

            if (pet.Birthdate.Year > 2000)
                cmd.Age = ((DateTime.Now.Year - pet.Birthdate.Year) * 12 + (DateTime.Now.Month - pet.Birthdate.Month)).ToString();
            cmd.Weight = pet.Weight;

            info.CMD = cmd;
            device.AddCommand(cmd);

            foreach (TabItem item in MainTag.Items)
                if ((IDevices)item.Tag == device || item.Tag == null)
                    (item.Content as UCTestResult).AddCmds(cmd);

            return true;
        }

        private bool CancelCmd(Command cmd)
        {
            if (BCL.WPFComponent.UCMessageBox.Show("确定要取消检查嘛？".Language(), "提示".Language(), MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                return false;
            if (cmd != null)
                cmd.Devices.RemoveCommand(cmd.Id);
            foreach (TabItem item in MainTag.Items)
                if ((IDevices)item.Tag == cmd.Devices || item.Tag == null)
                    (item.Content as UCTestResult).RemoveCmds(cmd);

            return true;
        }



        private void GridControl1_Selected(object sender, SelectedCellsChangedEventArgs e)
        {
            if (!(gridControl1.SelectedItem is TestInfoSource info))
                return;
            ucTestResults?.ForEach(o => o.SelectConnamd(info.CMD));
        }
    }
}
